﻿using MediatR;
using MyRabbit.Application.Command;
using MyRabbit.Application.ErrorCode;
using MyRabbit.Domain;
using MyRabbit.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRabbit.Application.CommandHandler
{
    public class ProductQueryCommandHandler : IRequestHandler<ProductQueryCommand, APIResult<List<Product>>>
    {
        /// <summary>
        /// 产品仓储
        /// </summary>
        private readonly IRepository<Product> productRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productRep"></param>

        public ProductQueryCommandHandler(IRepository<Product> productRep)
        {
            this.productRep = productRep;
        }


        public async Task<APIResult<List<Product>>> Handle(ProductQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Product>> result = new APIResult<List<Product>>();
            result.Code = Codes.查询成功;
            result.Message = Codes.查询成功.ToString();

            var list = await productRep.GetManyByConditionAsync(x => x.IsDeleted == false);

            result.Data = list;

            return await Task.FromResult(result);
        }
    }
}
